package org.csu.mypetstore.controller;

import org.csu.mypetstore.domain.Category;
import org.csu.mypetstore.domain.Item;
import org.csu.mypetstore.domain.Product;
import org.csu.mypetstore.service.CatalogService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;

import java.util.List;

@Controller
@RequestMapping("/catalog")
public class CatalogController {
    @Autowired
    private CatalogService catalogService;

    @GetMapping("/viewCategory")
    public String viewCategory(String categoryId, Model model){
        Category category = catalogService.getCategory(categoryId);
        List<Product> productList = catalogService.getProductListByCategory(categoryId);
        model.addAttribute("category",category);
        model.addAttribute("productList",productList);
        return "catalog/category";
    }
    @GetMapping("/viewProduct")
    public String viewProduct(String productId,Model model){
        Product product =catalogService.getProduct(productId);
        List<Item> itemList = catalogService.getItemListByProduct(productId);
        model.addAttribute("product",product);
        model.addAttribute("itemList",itemList);
        return "catalog/product";
    }
    @GetMapping("/itemManagement")
    public String itemmanagement(String productId,Model model){
        Product product = catalogService.getProduct(productId);

        List<Item> itemList = catalogService.getItemListByProduct(productId);
        model.addAttribute("itemList",itemList);
        model.addAttribute("product",product);
        return "management/petItem";
    }

    @GetMapping("/categoryManagement")
    public String categorymanagement(Model model){
        List<Category> categoryList = catalogService.getCategoryList();
        model.addAttribute("categoryList",categoryList);
        return "management/petCategory";

    }

    @GetMapping("/productManagement")
    public String productmanagement(String categoryId,Model model){
        Category category =catalogService.getCategory(categoryId);
        List<Product> productList = catalogService.getProductListByCategory(categoryId);
        model.addAttribute("category",category);
        model.addAttribute("productList",productList);
        return "management/petProduct";
    }
}
